/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-08-27 10:53:42
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-12-06 11:31:20
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import { MessageService } from '@abp/message/message.service';
import { finalize } from 'rxjs/operators';
import { LazyLoadEvent } from 'primeng/api';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { BomContentServiceProxy, BomEditChildData, BomDesignDetailDto, MaterialsServiceProxy, MaterialsEditContentDto } from '@shared/service-proxies/service-proxies';

@Component({
    selector: 'app-mount-details',
    templateUrl: './mount-details.component.html',
    styleUrls: ['./mount-details.component.less']
})
export class MountDetailsComponent extends AppComponentBase {
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Input() select: any;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    saving = false;
    active = false;
    selectRow: any = [];
    tableList = [];
    materialCode = undefined;//代号
    name = undefined;//名称
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _bomContentServiceProxy: BomContentServiceProxy,
        private _materialsServiceProxy: MaterialsServiceProxy,
    ) {
        super(injector);
    }
    resert() {
        this.materialCode = undefined;
        this.name = undefined;
    }
    ngOnInit() {
    }
    getTableList(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this._materialsServiceProxy.getmaterialsContentAll(
            undefined,
            undefined,
            this.name,
            this.materialCode,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }
    //打开弹窗
    onShown(): void {
    }
    //查看新增任务
    show(): void {
        this.active = true;
        this.modal.show();
    }
    //关闭事件
    close(): void {
        this.selectRow = [];
        //清空form表单
        this.active = false;
        this.modal.hide();
    }
    save() {
        for (const item of this.selectRow) {
            for (const son of this.select) {
                if (item.code == son.code) {
                    this._messageService.info(item.code+'已存在请勿添加相同的物料');
                    return;
                }
            }
        }
        for (const item of this.selectRow) {
            let guids = [];
                for (let i = 0; i < 8; i++) {
                    let guid = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
                    guids.push(guid);
                }
            let guidCode = guids[0] + guids[1] + "-" + guids[2] + "-" + guids[3] + "-" + guids[4] + "-" + guids[5] + guids[6] + guids[7];
            item.id = guidCode
        }
        this.modalSave.emit(this.selectRow);
        this.close();
    }
}
